package com.ld.admin.controller.timedtask;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.timedtask.SysTimedtaskModel;
import com.ld.admin.service.timedtask.SysTimedtaskService;
import com.ld.common.util.task.TaskManager;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 定时任务
 * 
 * @ClassName SysTimedtaskController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年01月04日 14:29:47
 *
 */
@Api(tags = "定时任务")
@ConditionalOnProperty(prefix = "modules.sc", name = "timedtask.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemSysTimedtaskController")
@RequestMapping("${shieldsb.adminPath}/system/timedtask")
public class SysTimedtaskController extends AdminBasicController<SysTimedtaskModel> {
    @Autowired
    protected SysTimedtaskService sysTimedtaskService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/system/timedtask/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/system/timedtask/systimedtask_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        sysTimedtaskService.doSearch(request, getModelClass());
        return "a/system/timedtask/systimedtask_list";
    }

    @ApiOperation(value = "定时任务展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        sysTimedtaskService.getOne(request, getModelClass(), id);
        return "a/system/timedtask/systimedtask_show";
    }

    @ApiOperation(value = "跳转到添加/修改定时任务", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        request.setAttribute("taskClasses", TaskManager.getTaskClses()); // 加载任务类
        sysTimedtaskService.getOne(request, getModelClass(), id);
        return "a/system/timedtask/systimedtask_update";
    }

    @ApiOperation(value = "添加/修改定时任务", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid SysTimedtaskModel model) {
        Result result = sysTimedtaskService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除定时任务", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = sysTimedtaskService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "启动/停止定时任务", notes = "")
    @RequestMapping(path = "/startOrStop", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object startOrStop() {
        Result result = sysTimedtaskService.startOrStop(getModelClass(), getLong("id"), getInt("isstart"));
        return Web.Response.result(result);
    }

    @ApiOperation(value = "获取执行时间", notes = "")
    @RequestMapping(path = "/getExcuteTime", method = RequestMethod.GET)
    // 返回json格式
    @ResponseBody
    public Object getExcuteTime() throws ServletException, IOException {
        String cron = getString("CronExpression");
        String type = getString("type");
        return Web.Response.resultWithData(sysTimedtaskService.getExcuteTime(cron, type, 6));
    }

}